package main

import (
	"gindemo/validate"
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.POST("test_page", func(c *gin.Context) {
		p := validate.GetPostForm(c)
		// 使用初始化的校验规则
		err := validate.V2(p, validate.CustomizeMap["PageVerify"])
		if err != nil {
			c.JSON(http.StatusOK, err.Error())
			return
		} else {
			c.JSON(http.StatusOK, gin.H{
				"msg": "success",
			})
			return
		}
	})
	r.POST("test_id", func(c *gin.Context) {
		p := validate.GetPostForm(c)
		// 使用自定义的规则
		err := validate.V2(p, map[string][]interface{}{
			"id": {validate.R_NotEmpty, validate.R_NGEZERO},
		})
		if err != nil {
			c.JSON(http.StatusOK, err.Error())
			return
		} else {
			c.JSON(http.StatusOK, gin.H{
				"msg": "success",
			})
			return
		}
	})
	r.POST("test_demo", func(c *gin.Context) {
		p := validate.GetPostForm(c)
		rule := validate.Rules{
			"username":  {validate.R_NotEmpty}, //非空
			"sex":       {validate.R_SEX},      // 只能是男女
			"is_retire": {validate.R_BOOL},     //bool
		}
		err := validate.V2(p, rule)
		if err != nil {
			c.JSON(http.StatusOK, err.Error())
		}
		c.JSON(http.StatusOK, "success")
	})
	r.Run(":9999")
}
